package com.laozhao.snow;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.laozhao.wintersystem.WSTools;

public class PageFactory {

//	private List<Action> actions;
	private List<String> paths;
//	private List<Element> elements;
	private Map<String,Node> nodeMap;
	
	public PageFactory(){
		
	}
	
	public void init(){
		for (String path : paths) {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			Element element = null;
			try {
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document doc = db.parse(new File((WSTools.getSystemRootPath()+path)));
				element = doc.getDocumentElement();
				
			} catch (ParserConfigurationException e) {
				e.printStackTrace();			
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (element != null) {
				nodeMap = new HashMap<String, Node>();
				NodeList nodes = element.getChildNodes();
				for (int i = 0; i < nodes.getLength(); i++) {
					org.w3c.dom.Node child = nodes.item(i);
					if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
						if (child.getNodeName().equalsIgnoreCase(System.ATTRIBUTE_NODE_NAME)){
							NodeList partList = child.getChildNodes();
							for (int j = 0; j < partList.getLength(); j++) {
								org.w3c.dom.Node part = partList.item(j);
								if (part.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
									nodeMap.put(WSTools.getStringAttribute(part, BaseNode.ATTRIBUTE_ID), part);
								}
							}
						}
					}
				}
			}
		}
	}
	
	public Object getPages(String id){
		return createObject(nodeMap.get(id));
	}
	
	private Object createObject(Node node){
		if (node != null) {
			String nodeName = node.getNodeName();
			if (Frame.ATTRIBUTE_NODE_NAME.equals(nodeName)) {
				return createFrame(node);
			}else if (Part.ATTRIBUTE_NODE_NAME.equals(nodeName)) {
				return createPart(node);
			}else if (Splash.ATTRIBUTE_NODE_NAME.equals(nodeName)) {
				return createSplash(node);
			}else if (Tray.ATTRIBUTE_NODE_NAME.equals(nodeName)) {
				return createTray(node);
			}else {
				return null;
			}
		}else {
			return null;
		}
	}
	
	private Tray createTray(Node node) {
		Tray tray =  buildNode(Tray.class,node);
		
		return tray;
	}

	private Object createSplash(Node node) {
		// TODO Auto-generated method stub
		return null;
	}

	private Object createPart(Node node) {
		// TODO Auto-generated method stub
		return null;
	}

	private Object createFrame(Node node) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("unchecked")
	private <T extends BaseNode> T buildNode(Class<T> clazz , Node node) {
		
		T base = null;
		if (node != null) {
			try {
				base = (T) Class.forName(WSTools.getStringAttribute(node,BaseNode.ATTRIBUTE_CLASS)).newInstance();
				base.setId(WSTools.getStringAttribute(node, BaseNode.ATTRIBUTE_ID));
				base.setName(WSTools.getStringAttribute(node, BaseNode.ATTRIBUTE_NAME));
				base.setIcon(WSTools.getStringAttribute(node, BaseNode.ATTRIBUTE_ICON));
				base.setDescription(WSTools.getStringAttribute(node, BaseNode.ATTRIBUTE_DESCRIPTION));
				NodeList nodes = node.getChildNodes();
				for (int i = 0; i < nodes.getLength(); i++) {
					org.w3c.dom.Node child = nodes.item(i);
					if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
						
					}
				}
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		return base;
	}
}
